<template>
	<view>
		<picker v-if="companyList.length>0" mode="selector" :range="companyList" range-key="addressName" @change="companyChange">
			<view>{{companyList[companyIndex].addressName}}</view>
		</picker>
		
		<button @tap="chooseImgs">+</button>
		<button @tap="doSubmit">提交</button>
		<image v-for="item in tempPicPaths" :src="item" :key="item" mode=""></image>
	</view>
</template>

<script>
	import {
		getPreOrderById
	} from '@/api/confirmOrder/index.js'
	import {
		getCompanyAddrss,
		addOrderReturnApply
	} from '@/api/user/order.js'
	import baseUrl  from '@/api/baseUrl.js'
	export default {
		name:"ReturnOrder",
		data() {
			return {
				companyIndex:0,
				orderId:'',
				companyList :[],
				tempPicPaths:[],
				orderDetail:{}
			};
		},
		onLoad(options){
			this.orderId = options.id
			this.getOrderDetail()
			this.getCompanyAddressList()
		},
		methods:{
			async getOrderDetail(){
				const res = await getPreOrderById(this.orderId)
				this.orderDetail = res.data
			},
			async getCompanyAddressList(){
				const res = await getCompanyAddrss()
				this.companyList = res.data.items
			},
			companyChange(e){
				// console.log(e);
				this.companyIndex = e.detail.value

			},
			chooseImgs(){
				var _this = this
				uni.chooseImage({
					sizeType:['original'],
					success(res){
						console.log(res);
						const tempFiles = res.tempFiles
						if(tempFiles.length>3){
							uni.showToast({
								title:"单次上传不能超过3张图片",
								icon:"none"
							})
							return 
						}
						// 判断图片大小
						// 1KB = 1024B
						// 1M = 1024KB 
						if(tempFiles.some(item=>item.size>1 * 1024 * 1024)){
							uni.showToast({
								title:"每个图片不能超过1M",
								icon:"none"
							})
							return 
						}
						// _this.tempPicPaths = res.tempFilePaths
						// _this.tempPicPaths = tempFiles.map(item=>item.path)
						if((_this.tempPicPaths.length + res.tempFilePaths.length) > 3){
							uni.showToast({
								title:"总个数不能超过3张图片",
								icon:"none"
							})
							return 
						}
						_this.tempPicPaths.push(...res.tempFilePaths)
						
					}
				})
			},
			doSubmit(){
				let pArr = []
				this.tempPicPaths.forEach(path=>{
					let p = new Promise((resove,reject)=>{
						// 异步操作
						uni.uploadFile({
							url: baseUrl + '/lejuClient/orderReturn/uploadImg',
							timeout:5000,
							filePath:path,
							name:'file',
							success(res) {
								// console.log("path res=>",JSON.parse(res.data).data.fileUrl);
								// picArr.push(JSON.parse(res.data).data.fileUrl)
								resove(res)
								
							},
							fail(error){
								reject(error)
							}
						})
					})
					pArr.push(p)
				})
				
				console.log("pArr =>",pArr);
				Promise.all(pArr)
				.then(res=>{
					console.log("res=>",res);
					let proofPics = res.map(item=>JSON.parse(item.data).data.fileUrl).join(',')
					addOrderReturnApply({
					  "companyAddressId": this.companyList[this.companyIndex].id,
					  "count": 1,
					  "description": "不喜欢",
					  "orderId": this.orderId,
					  "orderItemId": this.orderDetail.orderItems[0].id,
					  "proofPics": proofPics,
					  "reason": "质量太差"
					})
				})
				
			
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
